package com.lightsh.body;

import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.World;

import com.lightsh.gui.PhysicsScreen;
import com.lightsh.utility.GeometryUtility;

import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.Log;

class Polygon extends PhysicsBody{

	protected PolygonDef mPolygonDef;
	protected float[] mVertices;
	
	/**
	 * 创建一个多边形，顶点添加顺序为顺时针依次添加
	 * @param world 世界
	 * @param x 该图形的坐标原点x，作为之后{@link #add(Vec2, boolean)}的参考坐标
	 * @param y 该图形的坐标原点y，作为之后{@link #add(Vec2, boolean)}的参考坐标
	 * @param density 密度
	 * @param friction 摩擦力
	 * @param restitution 碰撞后恢复系数
	 */
	public Polygon(World world, float x, float y, float density,
			float friction, float restitution) {
		super(world);
		mPolygonDef = new PolygonDef();
		mPath = new Path();
		mPolygonDef.density = density;
		mPolygonDef.friction = friction;
		mPolygonDef.restitution = restitution;
		mPolygonDef.vertices.clear();
		mBodyDef.position.x = x;
		mBodyDef.position.y = y;
	}
	
	/**
	 * 需严格按顺时针依次添加，并且不能时凹形，否则出现质量为负数导致物体不动等非物理现象。
	 * <br/>注意，这里所添加的顶点的值，以body的position为参照物，即以{@link Polygon#Polygon(World, float, float, float, float, float, float)} 所设置的 x, y 值作为坐标系的原点(0, 0))，而非以world的坐标系作为参照
	 * <br/>
	 * <br/>例如下图， '。'为本Polygon所设置的x,y坐标,而三个'.'分别代边三角形的三个顶点,其中上顶点的坐标应该为(1，0)，假定该顶点与'。'水平并且向右偏移1的距离。
	 * <br/>	---------------------------------
	 * <br/>	|      。  .	            |
	 * <br/>	|                       |
	 * <br/>	|      .      .	        |
	 * <br/>	|                       |
	 * <br/>	----------------------------------
	 * 
	 * @param vec2 点向量
	 * @param last 是否最后一个向量
	 */
	public void add(Vec2 vec2, boolean bLast) {
		mPolygonDef.addVertex(vec2);
//		if (bLast) {
//			mVertices = new float[mPolygonDef.vertices.size() << 1];
//			mShape = mBody.createShape(mPolygonDef);
//			mBody.setMassFromShapes();
//		}
	}
	
	@Override
	public void draw(Canvas canvas, Rect screenRect, float selfScale, float worldScale) {
		super.draw(canvas, screenRect, selfScale, worldScale);		
		if(!isInRect(screenRect, selfScale)){
			return;
		}
		Vec2 vec2 = getPosition();
		canvas.save();
		rotateCanvas(canvas, mBody.getAngle(), vec2, screenRect, selfScale, worldScale);
		if(mSprite != null){
			mSprite.draw(canvas, mPaint, (int)((vec2.x ) / selfScale + mDrawImageOffsetX - screenRect.left), (int)((vec2.y) / selfScale + mDrawImageOffsetY- screenRect.top), worldScale);
		}else if(mBitmap != null){
			drawBitmap(canvas, vec2.x, vec2.y, screenRect, selfScale, worldScale);
		}else{
			GeometryUtility.trackPolygonPath(mPath, vec2, mPolygonDef.vertices, mVertices, selfScale, worldScale, screenRect.left, screenRect.top);
			canvas.drawPath(mPath, mPaint);
		}		
		canvas.restore();
	}

	@Override
	public void update() {
		super.update();
	}

	@Override
	public boolean isInRect(Rect rect, float selfScale) {
		Vec2 centerPoint = getPosition();
		return GeometryUtility.isPolygonInRect(centerPoint, mPolygonDef.vertices, rect, selfScale);
	}

	@Override
	public void create() {
		super.create();
		mBody = mWorld.createBody(mBodyDef);
		mVertices = new float[mPolygonDef.vertices.size() << 1];
		mShape = mBody.createShape(mPolygonDef);
		mBody.setMassFromShapes();
	}
	
	
}
